package cn.sofwin.blog.web;


import cn.sofwin.blog.entity.User;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 这是过滤器
 *
 * @author ZD
 * @version 1.0
 * @email 2466721401@qq.com
 */
@WebFilter({"/bolg/user/*", "/blog/article/form", "/blog/article/remove"})
public class LoginFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {

        String url = req.getServletPath();
        if ("/blog/user/login".equals(url) || "/blog/user/register".equals(url)) {
            // 向后放行
            chain.doFilter(req, resp);
        } else {
            User user = (User) req.getSession().getAttribute("LOGIN_USER");
            if (user != null) {
                chain.doFilter(req, resp);
            } else {
                // 用户未登录，重定向到登录页面
                resp.sendRedirect("/blog/user/login");
            }
        }
    }
}
